package com.aaa.sts.service.impl;

import com.aaa.common.entity.Station;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.StationVO;
import com.aaa.sts.dao.StationDao;
import com.aaa.sts.service.RemoteRoleService;
import com.aaa.sts.service.StationService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (Station)表服务实现类
 *
 * @author makejava
 * @since 2025-09-18 14:57:00
 */
@Service("stationService")
public class StationServiceImpl implements StationService {
    @Resource
    private StationDao stationDao;
    @Resource
    private HttpServletRequest request;
    @Resource
    private RemoteRoleService remoteRoleService;

    @Override
    public Station selectStationStatus(Integer storeId, Integer station) {
        return stationDao.selectStationStatus(storeId, station);
    }

    /**
     * 通过店铺id查询该店铺的工位信息
     *
     * @param storeId 店铺id
     * @return 店铺的洗车站信息
     */
    @Override
    public List<StationVO> queryByStoreId(Integer storeId) {
        return this.stationDao.queryByStoreId(storeId);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param station 主键
     * @return 实例对象
     */
    @Override
    public StationVO queryById(Integer station) {
        return this.stationDao.queryById(station);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<StationVO> page) {
        int userId = Integer.parseInt(TokenUtil.getUserId(request));
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        StationVO station = page.getData();
        List<String> roleKeyList = remoteRoleService.queryRoleKey(userId);
        String roleKey = roleKeyList.contains("guanliyuan") ? "guanliyuan" :
                roleKeyList.contains("laoban") ? "laoban" : null;
        if (roleKey.equals("laoban")){
            station.setUserId(userId);
        }
        return new PageInfo(stationDao.queryAllByLimit(station));
    }

    /**
     * 新增数据
     *
     * @param station 实例对象
     * @return 实例对象
     */
    @Override
    public Station insert(Station station) {
        this.stationDao.insert(station);
        return station;
    }

    /**
     * 修改数据
     *
     * @param station 实例对象
     * @return 实例对象
     */
    @Override
    public StationVO update(StationVO station) {
        this.stationDao.update(station);
        return this.queryById(station.getStation());
    }

    /**
     * 通过主键删除数据
     *
     * @param station 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer station) {
        return this.stationDao.deleteById(station) > 0;
    }
}
